home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / update-openoffice-dicts < prev    next >
Encoding:
Text File  |  2006-12-19  |  4.9 KB  |  169 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # update-openoffice-dicts -- update OpenOffice.org's dictionary.lst
  4. # (c) 2003 RenΘ Engelhard <rene@debian.org>
  5. #          Agustin Martin Domingo <agmartin@debian.org>
  6. # Public Domain
  7.  
  8.  
  9. @print_comments=("## List of All Dictionaries to be Loaded by OpenOffice.org",
  10. "## ---------------------------------------------------",
  11. "## Each Entry in the list have the following space delimited fields",
  12. "##",
  13. "## Field 1: Entry Type \"DICT\" - spellchecking dictionary",
  14. "##                     \"HYPH\" - hyphenation dictionary",
  15. "##                     \"THES\" - thesaurus files",
  16. "##",
  17. "## Field 2: Language code from Locale \"en\" or \"de\" or \"pt\" ...",
  18. "##",
  19. "## Field 3: Country Code from Locale \"US\" or \"GB\" or \"PT\"",
  20. "##",
  21. "## Field 4: Root name of file(s) \"en_US\" or \"hyph_de\" or \"th_en_US\"",
  22. "##          (do not add extensions to the name)",
  23. "##",
  24. "## This file is automatically updated by update-openoffice-dicts script",
  25. "##",
  26. "");
  27.  
  28. # put all entries not in the automatic section in the new dictionary.lst
  29. # file (manually added dictionaries by the user)
  30. sub write_manually_added() {
  31.     my $line;
  32.     my $inheader = 1;
  33.     open(DL_ORIG, "$dictlist_orig") # open the existing file for reading...
  34.     or die("Opening $dictlist_orig failed.\n");
  35.     while (<DL_ORIG>) {
  36.     chomp;
  37.     $line = $_;
  38.     undef $inheader if not m/^\#/;
  39.     
  40.     push @dictionary_orig,$line; # save original file
  41.     
  42.     if ($line eq $begin_string) {
  43.         # if we find the start of the section; set the flag that we are
  44.         # in the automatic section, ignore the following lines ...
  45.         $we_are_in_auto=1;
  46.         next;
  47.     } elsif ($line eq $end_string) {
  48.         # ... until we find the end and set the flag back.
  49.         $we_are_in_auto=0;
  50.         next;
  51.     } elsif ($we_are_in_auto == 0) {
  52.         # we are in non-automatic section; copy the user added entries
  53.         # into the temporary array; but that only if the line is not a ##
  54.         # comment or no blank line
  55.         next if $line =~ /^\#\#/;
  56.         next if $line =~/^\s*$/;
  57.         if ($inheader) {
  58.         next if $line =~ /^\#/;
  59.         }
  60.         push @dictionary_lst, $line;
  61.     }
  62.     }
  63.     close(DL_ORIG);
  64. }
  65.  
  66. # read all files in $entrydir and put their contents verbatim in
  67. # the new dictionary.lst
  68. sub write_automatic_section() {
  69.     push @dictionary_lst, "$begin_string";
  70.     push @dictionary_lst, "$str_instruct_add";
  71.     foreach $file (<$entrydir/*>) {
  72.     open(ENTRY,"$file");
  73.     while (<ENTRY>){
  74.         chomp;
  75.         push @dictionary_lst, $_;
  76.     }
  77.     close(ENTRY);
  78.     }
  79.     push @dictionary_lst, "$end_string", "";
  80. }
  81.  
  82. sub write_dictionary_lst() {
  83.     push @dictionary_lst, @print_comments;
  84.     write_automatic_section();
  85.  
  86.     if ( -e $dictlist_orig ) {
  87.             $we_are_in_auto=0;
  88.             write_manually_added();
  89.     }
  90.  
  91.     open(NEWFILE, "> $dictlist_orig") # open the existing file for writing.
  92.             or die("Opening $dictlist_orig failed.\n");
  93.     foreach (@dictionary_lst) { print NEWFILE $_, "\n"; }
  94.     close(NEWFILE);
  95.  
  96.     open(OLDFILE, "> $dictlist_orig.old") # open the backup file for writing
  97.             or die("Opening $dictlist_orig.old failed.\n");
  98.     foreach (@dictionary_orig) { print OLDFILE $_, "\n"; }
  99.     close(OLDFILE);
  100.  
  101.     # make sure the file has the right permissions
  102.     chown(0, 0, $dictlist_orig);
  103.     chmod(0644, $dictlist_orig);
  104. }
  105.  
  106. # --------------------------------------------------------------------
  107. # The main program
  108. # --------------------------------------------------------------------
  109.  
  110. $dictlist_orig    = "/etc/openoffice/dictionary.lst";
  111. $entrydir         = "/usr/share/myspell/infos/ooo";
  112. $begin_string     = "## !!! BEGIN AUTOMATIC SECTION -- DO NOT CHANGE !!!";
  113. $end_string       = "## !!! END AUTOMATIC SECTION -- DO NOT CHANGE !!!";
  114. $str_instruct_add = "## !!! ADD YOUR ADDITIONAL ENTRIES BELOW THIS SECTION !!!";
  115. @dictionary_orig  = (); # The array with the original strings from dictionary.lst
  116. @dictionary_lst   = (); # The array with the strings to go to new dictionary.lst
  117.  
  118. die "$0: This script needs root privileges...\n" if ($< != 0);
  119.  
  120. print STDOUT "Updating OpenOffice.org's dictionary list... ";
  121.  
  122. # we may need to create the dir...
  123. unless ( -d "/etc/openoffice" ) {
  124.     mkdir("/etc/openoffice",0755) || die "can't mkdir /etc/openoffice: $!\n";;
  125. }
  126.  
  127. write_dictionary_lst();
  128.  
  129. print STDOUT "done.\n";
  130.  
  131. __END__
  132.  
  133. =head1 NAME
  134.  
  135. update-openoffice-dicts - rebuild dictionary.lst for OpenOffice.org
  136.  
  137. =head1 SYNOPSIS
  138.  
  139. update-openoffice-dicts
  140.  
  141. =head1 DESCRIPTION
  142.  
  143. update-openoffice-dicts can be used to regenerate dictionary.lst for
  144. openoffice.  This script is run by myspell dictionaries when they are
  145. installed or removed.
  146.  
  147. It is not normally necessary to run update-openoffice-dicts by hand. 
  148. Packages containing dictionaries create a file in /usr/share/myspell/infos
  149. and call this script automatically.
  150.  
  151. update-openoffice-dicts will create a new dictionary.lst if it does not
  152. already exist.
  153.  
  154. =head1 FILES
  155.  
  156. /etc/openoffice/dictionary.lst, /usr/share/myspell/infos/ooo/*
  157.  
  158. =head1 SEE ALSO
  159.  
  160. openoffice(1), dictionary.lst(5)
  161.  
  162. =head1 AUTHORS
  163.  
  164. Rene Engelhard, Agustin Martin Domingo
  165.  
  166. =cut
  167.  
  168. #  LocalWords:  openoffice myspell dicts
  169.